package com.nowcoder.community.service;

import com.nowcoder.community.dao.DiscussPostMapper;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;
    /**
     * 分页查询所有帖子
     * @author ProgramCat
     * @param userId 帖子的用户id（等于0不使用）
     * @param offset 起始行行号
     * @param limit 每页数量
     * @return List<DiscussPost> 帖子集合
     */
    public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit) {
        return discussPostMapper.selectDiscussPosts(userId, offset, limit);
    }
    /**
     * 查询表中一共有多少条数据
     * @author ProgramCat
     * @param userId 用户id （等于0不使用）
     * @return int
     */
    public int findDiscussPostRows(int userId) {
        return discussPostMapper.selectDiscussPostRows(userId);
    }
    /**
     *添加新的帖子
     * @author ProgramCat
     * @param post 帖子实体
     * @return int
     */
    public int addDiscussPost(DiscussPost post){
        if(post==null){
            throw new IllegalArgumentException("参数不能为空");
        }
        //转义HTML标记
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        //过滤敏感词汇
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));
        return discussPostMapper.insertDiscussPost(post);
    }
    /**
     * 根据帖子id 查询单个帖子内容
     * @author ProgramCat
     * @param id  帖子id
     * @return DiscussPost
     */
    public DiscussPost findDiscussPostById(int id){
        return discussPostMapper.selectDiscussPostById(id);
    }

    /**
     * 根据帖子id 修改 帖子内容
     * @author ProgramCat
     * @param id 帖子id
     * @param commentCount 帖子内容
     * @return int
     */
    public int  updateCommentCount(int id,int commentCount){
        return discussPostMapper.updateCommentCount(id,commentCount);
    }
}
